﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaiser.Modules.Mis.Repositories;
using System.Collections;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using Kaiser.Models.JsonModels;
using Kaiser.Models;
using Kaiser.Modules.Mis.Models;
using Kaiser.Extensions;
namespace Kaiser.Repositories.iBatis
{
    public class BookRepository : IBookRepository
    {
        private IBatisHepler IBatisHepler = null;
        public BookRepository() {
            IBatisHepler = new BatisHepler("SQLMap.MIS.config");
        }
        #region 图书管理
        public DataGrid<BookInput> SearchBook(BookSearch search, PagingInfo pageInfo)
        {
            var result = new DataGrid<BookInput>();
            Hashtable parms = new Hashtable();
            parms["BookCategoryCode"] = search.BookCategoryCode;
            parms["BookName"] = search.BookName;
            parms["BookCategoryName"] = search.BookCategoryName;
            int totalCount = 0;
            
           result.TableResult = IBatisHepler.ExcuteSelect("GetBookList", parms, pageInfo.Size, pageInfo.Index+1, out totalCount);
          
            result.total = totalCount;

            return result;
        }


        public void InsertBook(BookInput model)
        {
            Hashtable parms = new Hashtable();
            parms["BookID"] = model.BookID;
            parms["BookName"] = model.BookName;
            parms["BookCategoryName"] = model.BookCategoryName;
            parms["BookCategoryCode"] = model.BookCategoryCode;
            parms["BookAuthor"] = model.BookAuthor;
            parms["BookPublisherCode"] = model.BookPublisherCode;
            parms["BookPublisherName"] = model.BookPublisherName;

            var result= IBatisHepler.ExcuteInsert("AddBook", parms);
        }


        public void UpdateBook(BookInput model)
        {
            Hashtable parms = new Hashtable();
            parms["BookID"] = model.BookID;
            parms["BookName"] = model.BookName;
            parms["BookCategoryName"] = model.BookCategoryName;
            parms["BookCategoryCode"] = model.BookCategoryCode;
            parms["BookAuthor"] = model.BookAuthor;
            parms["BookPublisherCode"] = model.BookPublisherCode;
            parms["BookPublisherName"] = model.BookPublisherName;

            var result = IBatisHepler.ExcuteUpdate("UpdateBook", parms);
        }

        public int DeleteBookList(string ids)
        {
            Hashtable parms = new Hashtable();
            parms["BookCodeList"] = ids.Split(',');
            var result = IBatisHepler.ExcuteDelete("DeleteBookList", parms);
            return result;
        }

        #endregion

        public DataGrid<CategoryInput> SearchCategory(CategorySearch search, PagingInfo pageInfo)
        {
            var result = new DataGrid<CategoryInput>();
            Hashtable parms = new Hashtable();
 
            int totalCount = 0;

            result.TableResult = IBatisHepler.ExcuteSelect("GetCategoryAll", search, pageInfo.Size, pageInfo.Index + 1, out totalCount);

            result.total = totalCount;

            return result;
        }

        public void InsertCategory(CategoryInput model)
        {


            var result = IBatisHepler.ExcuteInsert("InsertCategory", model);
        }

        public void UpdateCateogry(CategoryInput model)
        {
            var result = IBatisHepler.ExcuteUpdate("UpdateCategory", model);
        }


        public DataGrid<CategoryInput> GetCategoryTreeList(CategorySearch search, PagingInfo pageInfo)
        {
            var result = new DataGrid<CategoryInput>();
            Hashtable parms = new Hashtable();
            parms["CategoryParentCode"] = search.CategoryParentCode;
            int totalCount = 0;

            result.TableResult = IBatisHepler.ExcuteSelect("GetCategoryTreeList", parms, pageInfo.Size, pageInfo.Index + 1, out totalCount);

            result.total = totalCount;

            return result;
        }




        public int DeletePublisherList(string ids)
        {
            Hashtable parms = new Hashtable();
            parms["PublisherCodeList"] = ids.Split(',');
            var result = IBatisHepler.ExcuteDelete("DeletePublisherList", parms);
            return result;
        }

        public int DeleteCategoryList(string ids)
        {
            Hashtable parms = new Hashtable();
            parms["CategoryCodeList"] = ids.Split(',');
            var result = IBatisHepler.ExcuteDelete("DeleteCategoryList", parms);
            return result;
        }


        #region 出版社模块

        private Hashtable GetPublisherHashTable(PublisherInput model)
        {
            Hashtable parms = new Hashtable();
            parms["PublisherCode"] = model.PublisherCode;
            parms["PublisherName"] = model.PublisherName;
            parms["PublisherUrl"] = model.PublisherUrl;
            return parms;
        }
        public void UpdatePublisher(PublisherInput model)
        {
            Hashtable parms = GetPublisherHashTable(model);
            var result = IBatisHepler.ExcuteUpdate("UpdatePublisher", parms);
        }

        public void InsertPublisher(PublisherInput model)
        {
            Hashtable parms = GetPublisherHashTable(model);
            var result = IBatisHepler.ExcuteInsert("InsertPublisher", parms);
        }


        public DataGrid<PublisherInput> SearchPublisher(PublisherSearch search, PagingInfo pageinfo)
        {
            var result = new DataGrid<PublisherInput>();
            Hashtable parms = new Hashtable();
            parms["PublisherName"] = search.PublisherName;
            int totalCount = 0;

            result.TableResult = IBatisHepler.ExcuteSelect("GetPublisherList", parms, pageinfo.Size, pageinfo.Index + 1, out totalCount);

            result.total = totalCount;

            return result;
        }

        #endregion
    }
}
